//
//  ZJMessageCell.m
//  QQ_Chat_UI
//
//  Created by ZJ on 2018/7/31.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJMessageCell.h"
#import "ZJMessageFrame.h"
#import "ZJMessage.h"

@interface ZJMessageCell ()

@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UIButton *textBtn;

@end

@implementation ZJMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupGUI];
    }
    
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self setupGUI];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupGUI {
    // 显示时间Label
    UILabel *timeLbl = [[UILabel alloc] init];
    
    timeLbl.font = [UIFont systemFontOfSize:12];
    timeLbl.textAlignment = NSTextAlignmentCenter;
    
    [self.contentView addSubview:timeLbl];
    _timeLabel = timeLbl;
    
    // 头像
    UIImageView *iconImgView = [[UIImageView alloc] init];
    [self.contentView addSubview:iconImgView];
    _iconImgView = iconImgView;
    
    // 正文
    UIButton *contentBtn = [[UIButton alloc] init];
    
    contentBtn.titleLabel.font = [UIFont systemFontOfSize:ZJMessageFrameDefaultFontSize];
    [contentBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    contentBtn.titleLabel.numberOfLines = 0;
    
    contentBtn.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);
    
    [self.contentView addSubview:contentBtn];
    _textBtn = contentBtn;
    
    // 设置cell背景颜色
    self.backgroundColor = [UIColor clearColor];
}

- (void)setMessageFrame:(ZJMessageFrame *)messageFrame {
    _messageFrame = messageFrame;
    
    ZJMessage *message = messageFrame.message;
    
    self.timeLabel.text = message.time;
    self.timeLabel.frame = messageFrame.timeFrame;
    self.timeLabel.hidden = message.hideTime;
    
    // 设置头像数据
    self.iconImgView.image = [UIImage imageNamed:message.iconImgName];
    self.iconImgView.frame = messageFrame.iconFrame;
    
    // 设置正文
    [self.textBtn setTitle:message.text forState:UIControlStateNormal];
    self.textBtn.frame = messageFrame.textFrame;
    
    // 设置正文的背景图
    NSString *imageNorName = nil;
    NSString *imageHightedName = nil;
    
    if (message.type == ZJMessageTypeMe) {
        imageNorName = @"chat_send_nor";
        imageHightedName = @"chat_send_press_pic";
        
        [self.textBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        imageNorName = @"chat_recive_nor";
        imageHightedName = @"chat_recive_press_pic";
        
        [self.textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    UIImage *imageNormal = [UIImage imageNamed:imageNorName];
    UIImage *imageHighted = [UIImage imageNamed:imageHightedName];
    
    imageNormal = [imageNormal stretchableImageWithLeftCapWidth:imageNormal.size.width * 0.5 topCapHeight:imageNormal.size.height * 0.5];
    imageHighted = [imageHighted stretchableImageWithLeftCapWidth:imageHighted.size.width * 0.5 topCapHeight:imageHighted.size.height * 0.5];
    
    [self.textBtn setBackgroundImage:imageNormal forState:UIControlStateNormal];
    [self.textBtn setBackgroundImage:imageHighted forState:UIControlStateHighlighted];
}

@end
